package com.bzkj.basicinfo.service.basic.impl;

import com.bzkj.basicinfo.domain.basic.WmsRuleEntity;
import com.bzkj.basicinfo.domain.basic.dto.WmsRuleDto;
import com.bzkj.basicinfo.domain.basic.vo.RuleMapVo;
import com.bzkj.basicinfo.domain.basic.vo.WmsRuleVo;
import com.bzkj.basicinfo.mapper.WmsRuleMapper;
import com.bzkj.basicinfo.service.basic.WmsRuleService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author wl
 * @description 针对表【basic_sto_stock_rule(仓库规则表)】的数据库操作Service实现
 * @createDate 2025-01-13 20:13:06
 */
@Slf4j
@Service
public class WmsRuleServiceImpl implements WmsRuleService
{

    @Autowired
    private WmsRuleMapper wmsRuleMapper;

    /**
     * 获取所有规则
     *
     * @return
     */
    @Override
    public R findRuleAll()
    {
        List<WmsRuleEntity> list = wmsRuleMapper.findRuleAll();

        ArrayList<WmsRuleEntity> entities = new ArrayList<>();

        Map<Integer, WmsRuleEntity> collect = list.stream().map(item -> {
            if(item.getParentId().equals(0)) entities.add(item);
            return item;
        }).collect(Collectors.toMap(WmsRuleEntity :: getId, Function.identity()));

        list.stream().forEach(item -> {
            WmsRuleEntity temp = collect.get(item.getParentId());
            WmsRuleVo vo = new WmsRuleVo();
            BeanUtils.copyProperties(item, vo);
            if(! Objects.isNull(temp)) temp.getChildren().add(vo);
        });

        return R.ok(entities);
    }

    /**
     * 关联数据
     *
     * @param ruleId
     * @param association
     * @param type
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public AjaxResult associationRule(WmsRuleDto dto, Integer type)
    {
        try {
            if(dto.getAssociation().isEmpty() || dto.getRuleId().isEmpty()) return AjaxResult.error("请选择关联类型");

            dto.getAssociation().stream().forEach(item -> {
                RuleMapVo temp = new RuleMapVo();
                temp.setAssociation(item);
                temp.setType(type);
                dto.getRuleId().stream().forEach(item1 -> {
                    temp.setRule_id(item1);
                    if(Objects.isNull(wmsRuleMapper.findRuleIsExits(temp))) {
                        wmsRuleMapper.associationRule(temp);
                    }
                });
            });

            return AjaxResult.success("添加成功");
        } catch(Exception e) {
            log.error("关联数据失败:{}", e);
            return AjaxResult.error("关联数据");
        }
    }

    /**
     * 修改状态 关闭
     *
     * @param id
     * @return
     */
    @Override
    public R putRuleEnableStatus(Integer id)
    {
        try {
            wmsRuleMapper.putRuleEnableStatus(id);
            return R.ok("修改成功");
        } catch(Exception e) {
            log.error("修改失败:{}", e);
            return R.fail("修改失败");
        }
    }

    /**
     * 获取关联的规则数据时
     *
     * @param type 规则类型
     * @return
     */
    @Override
    public List<HashMap> getRuleData(Integer type)
    {
        List<HashMap> list = null;
        if(type == 1) list = wmsRuleMapper.getStockRuleData(type);

        return list;
    }

    /**
     * 保存规则信息
     *
     * @param wmsRuleEntity
     * @return
     */
    @Override
    public AjaxResult saveRuleInfo(WmsRuleEntity wmsRuleEntity)
    {
        try {
            wmsRuleMapper.saveRuleInfo(wmsRuleEntity);
            return AjaxResult.success("操作成功");
        } catch(Exception e) {
            log.error("保存失败:{}", e);
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 修改规则状态
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R putRuleInfo(Integer id)
    {
        try {
            wmsRuleMapper.putRuleEnableStatusInfo(id);
            wmsRuleMapper.putRuleInfo(id);
            return R.ok("更新成功");
        } catch(Exception e) {
            log.error("修改失败:{}", e);
            return R.fail("修改失败");
        }
    }

    /**
     * 修改规则
     * @param wmsRuleEntity
     * @return
     */
    @Override
    public R putRule(WmsRuleEntity wmsRuleEntity)
    {
        wmsRuleMapper.putRule(wmsRuleEntity);
        return R.ok("修改成功");
    }
}




